package com.wave.wavesystem.develop.codeGenetrate.pojo.wrapper;


import com.wave.wavesystem.common.util.Func;

/**
 * @author 夕下风波
 * @date 2024-12-21 19:42
 * @description mysql 字段类型解析包装类
 */
public class TableFieldWrapper {

    /**
     * 字段包装
     *
     * @param fieldTypeName 字段类型名称
     * @return Java 类型
     */
    public static String fieldPropertyType(String fieldTypeName) {
        switch (fieldTypeName) {
            case "date" -> {
                return "LocalDate";
            }
            case "datetime" -> {
                return "LocalDateTime";
            }
            case "varchar", "text" -> {
                return "String";
            }
            case "char" -> {
                return "Character";
            }
            case "decimal" -> {
                return "BigDecimal";
            }
            case "tinyint", "int" -> {
                return "Integer";
            }
            case "tinyint(1)" -> {
                return "Boolean";
            }
            case "bigint" -> {
                return "Long";
            }
        }
        return "";
    }

    /**
     * 字段驼峰 转换
     *
     * @param fieldName 字段名称
     * @return 驼峰转换后的名称
     */
    public static String fieldNameToHumpType(String prefix, String fieldName, Boolean ignoreFirst) {
        int current = 0;
        String replacedName = fieldName;
        if (Func.isNotEmpty(prefix) && Func.isNotBlank(prefix)) {
            replacedName = fieldName.replaceFirst(prefix, "");
        }
        String[] splitName = replacedName.split("_");
        StringBuilder humpName = new StringBuilder();
        for (String split : splitName) {
            if (split.length() == 1) {
                humpName.append(split.toUpperCase());
                continue;
            }
            char upperCase;
            if (Func.isTrue(ignoreFirst) && (current == 0)) {
                upperCase = Character.toLowerCase(split.charAt(0));
            } else {
                upperCase = Character.toUpperCase(split.charAt(0));
            }
            String left = split.substring(1);
            humpName.append(upperCase)
                    .append(left);
            current++;
        }
        return humpName.toString();
    }

}
